<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>简易自定义表单及拖拽排序</title>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-sortable-min.js"></script>

<!--公共样式和图标样式-->
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<style>
body,html{margin:0;background:#eee;overflow-X:hidden}
body.dragging,body.dragging *{cursor:move!important}
.dragged{position:absolute;opacity:.5;z-index:2000}
.wrap{width:500px;margin:0 auto;padding:50px 10px 10px 10px;background:#fff;height:100%;position:relative}
.wrap .addbtns{-ms-transform:translate(510px,100px);-webkit-transform:translate(510px,100px);-o-transform:translate(510px,100px);-moz-transform:translate(510px,100px)}
.wrap .addbtns button{margin-top:20px}
ul.customerlist{width:300px;min-height:400px;margin:-150px auto 0 auto;padding-left:0}
ul.customerlist .tips{text-align:center;line-height:400px;font-size:16px;color:#aaa}
ul.customerlist li{min-height:50px;line-height:40px;margin:10px 0;box-shadow:0 0 20px #ddd;list-style:none;margin-left:0;padding:10px}
ul.customerlist li .delete{color:rgba(255,0,0,.6);background:#eee;padding:5px 10px;right:2px;top:2px;border-radius:5px;z-index:2;line-height:15px;cursor:pointer;font-size:12px;float:right;transition:all ease .3s}
ul.customerlist li .delete:hover{background:#ddd;color:rgba(255,0,0,1)}
ul.customerlist li.placeholder{position:relative;visibility:hidden;transition:all 1s ease}
ul.customerlist li.placeholder:before{position:absolute}
ul.customerlist li.dragged{box-shadow:0 0 10px #999}
ul.customerlist li .customeritem{cursor:pointer}
ul.customerlist li .customeritem .value{border:1px solid #ddd;border-radius:3px;height:40px;color:#969696;padding-left:5px}
ul.customerlist li .customeritem .value.textarea{height:80px}
ul.customerlist li .customeritem .value.file{height:100px;width:100px;line-height:100px;text-align:center}
</style>

</head>

<body>

<div class="wrap">
	<h3 style=" text-align:center;">简易自定义表单及拖拽排序</h3>
	<div  class="addbtns">
		<button type="button" class="add btn btn-warning" data-target="_oneline"><span class="fa fa-plus"></span> 单行输入框</button><br/>
		<button type="button" class="add btn btn-warning" data-target="_multiline" ><span class="fa fa-plus"></span> 多行输入域</button><br/>
		<button type="button" class="add btn btn-warning" data-target="_file" ><span class="fa fa-plus"></span> 附 件</button>
	</div>


	<!-- 控件源代码 -->
	<ul class="originlist" style="display:none;">
		<li class="_oneline">
			<span class="delete fa fa-trash-o"> 删除</span>
			<div class="customeritem">
				<span class="key" data-title="标题">标题</span> 
				<span class="fa fa-edit"></span>
				
			</div>
		</li>

		<li class="_multiline">
			<span class="delete fa fa-trash-o">删除</span> 
			<div class="customeritem">
				<span class="key" data-title="说明">说明</span> 
				<span class="fa fa-edit"></span>
				<div class="value textarea" data-type="textarea" data-placeholder="请填写说明">请填写说明</div>
			</div>
		</li>
		<li class="_file">
			<span class="delete fa fa-trash-o">删除</span>
			<div class="customeritem">
				<span class="key" data-title="附件">附件</span> 
				<span class="fa fa-edit"></span>
				<div class="value file" data-type="file" data-placeholder="请选择附件"><span class="fa fa-plus"></span></div>
			</div>
		</li>
	</ul>


	<ul class="customerlist">
		<p class="tips">请点击右侧按钮,添加自定义控件</p>
	</ul>
	<button type="button" class="showjson  btn btn-block btn-success" ><span class="fa fa-save"></span> 保 存</button>
	<div class="console" >

	</div>
</div>

<script>
	$(function () {

		$('.add').on('click', function () {

			var that = $(this);

			var target = that.data('target');

			$('.customerlist').append('<li> ' + $('.originlist>.' + target).html() + '</li>');

			$("ul.customerlist").sortable();

			//off 先取消绑定，否则会调用多次
			$('.customeritem').off('click').on('click', modifytitle);
			$('.delete').off('click').on('click',deleteitem);

			$('.tips').hide();

		})

		$('.showjson').on('click',function(){

			if($('.customerlist .customeritem').length==0) return;

			var temp=[];
			var t;
			$('.customerlist .customeritem').each(function(index,element){
				t = $(this).children();
				temp.push({"type":$(t[2]).data('type'),"title":$(t[0]).data('title')});
			})
			console.log(temp);
			$('.console').html(JSON.stringify(temp))
		})

	})

	function modifytitle(e) {

		var that = $(this);
		var key = that.find('.key').eq(0);
		var value = that.find('.value').eq(0);

		var newtitle = prompt("请填写标题", key.html());
		if ($.trim(newtitle).length >55) {
			alert('标题长度不能超过8位');
		}else if ($.trim(newtitle).length > 0) {
			key.data('title',newtitle).html(newtitle) ;
			if(value.data('type')!='file') value.data('placeholder',newtitle).html( '请填写' + newtitle);
		}else{}
	}

	function deleteitem() {
		if(confirm('确定要删除吗？')){
			var that = $(this);
			var parent = that.parent();
			parent.remove();
			if($('.customerlist .customeritem').length==0) $('.tips').show();
		}

	}
</script>

</body>
</html>